#include "types.h"
#include "screen.h"
#include "idt_gdt.h"
#include "font.h"
#include "x86.h"
#include "interrupt.h"
#include "print.h"

typedef void (*fpt)(uint8_t color);
extern void test_window(char color);

/**
 * @note 其他函数实现不能在os_main前面，否则会直接运行该函数，os_main将不起作用，原因不确定，后续再在学习中了解
 *
 */
void os_main(void)
{
    static char font[40]; // sprintf buffer
    int i = 124567;       // sprintf variable i for test
    int8_t mouse_pic[256];   // mouse logo buffer
    boot_info_t *boot_info = (boot_info_t *)ADDR_BOOT;

    clear_screen(0xf);
    sti(); // enable cpu interrupt
    init_screen(boot_info);
    init_palette();
    draw_window(); // color table from 0 to 15

    // display mouse logo
    init_mouse(mouse_pic, 7); // 7　means background color:white
    display_mouse(boot_info->vram, boot_info->x_size, 16, 16, 60, 60, mouse_pic, 16);
    init_gdt_idt();
    init_pic();           //函数中：　irq 1(keyboard)对应设置中断号int0x21,    irq　12(mouse)对应的中断号是int0x2c 要写中断服务程序了。
    outb(PIC0_IMR, 0xf9); // 1111 1001  irq 1 2打开 因为keyboard是irq 1
    outb(PIC1_IMR, 0xef); // 1110 1111  irq 12打开　mouse是irq 12  所以要把pic 1 pic 2的芯片中断响应位打开。

    //打印出int0x21处的idt值，主要看offset是否与　asm_int_handler_21一样（看反汇编，可以看到地址）
    fpt cls = clear_screen;
    (*cls)(5);
    int *addr = (int *)(0x0026f800 + 8 * 0x21);
    print_debug(*(addr), 0);
    print_debug(*(addr + 1), 160);

    while (1)
        ;
}

void test_window(char color)
{
    uint8_t *p = (uint8_t *)VRAM;

    for (int i = 0; i <= 0xffff; i++)
    {
        p[i] = color;
    }
}
